
> # set up environment ----
> library(data.table)

> load("data/analysis_data.RData")

> summarize <- function(v, x) {
+   data.table(
+     variable = v,
+     mean = mean(x),
+     sd = sd(x),
+     min = min(x),
+     max = max(x))
+ }

> print_rows <- function(x, digits) {
+   for (i in 1:nrow(x)) {
+     for (k in 1:ncol(x)) {
+       fmt <- paste0("%.0", digits[k], "f")
+       if (k == 1) {
+         cat(x[i, k, with = FALSE][[1]])
+       } else {
+         cat(" $", sprintf(fmt, x[i, k, with = FALSE][[1]]), "$ ", sep = "")
+       }
+       if (k == ncol(x)) {
+         cat("\\\\\n")
+       } else {
+         cat(" & ", sep = "")
+       }
+     }
+   }
+ }

> X <- respondent_data[, rbind(
+   summarize("Age 18-24", as.numeric(age_category == "18-24")),
+   summarize("Age 25-34", as.numeric(age_category == "25-34")),
+   summarize("Age 35-44", as.numeric(age_category == "35-44")),
+   summarize("Age 45-64", as.numeric(age_category == "45-64")),
+   summarize("Age Over 65", as.numeric(age_category == "Over 65")),
+   summarize("Did not Vote/Supp. Other Cand. in 2016", as.numeric(support_2016 == "other/no one")),
+   summarize("Supported Clinton in 2016", as.numeric(support_2016 == "Clinton")),
+   summarize("Supported Sanders in 2016", as.numeric(support_2016 == "Sanders")),
+   summarize("Supported Trump in 2016", as.numeric(support_2016 == "Trump")),
+   summarize("HS or Less", as.numeric(education == "HS or Less")),
+   summarize("Some College", as.numeric(education == "Some College")),
+   summarize("College Graduate", as.numeric(education == "College Grad.")),
+   summarize("Black", as.numeric(race_ethnicity == "Black")),
+   summarize("Latino", as.numeric(race_ethnicity == "Latino")),
+   summarize("Other/Mixed Race/Ethnicity", as.numeric(race_ethnicity == "Other/Mixed")),
+   summarize("White", as.numeric(race_ethnicity == "White")),
+   summarize("Female", female),
+   summarize("Campaign Knowledge", campaign_knowledge),
+   summarize("Follows Politics", follows_politics),
+   summarize("Strong Democrat", strong_democrat),
+   summarize("Trump Least Preferred", trump_least_preferred),
+   summarize("Intend to Vote", vote_2020),
+   summarize("Registered Voter", registered_voter),
+   summarize("Ideology = ideology", ideology),
+   summarize("Political Interest", political_interest),
+   summarize("# Failed Attention Checks", n_attention_fails),
+   summarize("Need for Cognition", need_for_cognition))]

> print_rows(X, c(0, 2, 2, 1, 1))
Age 18-24 &  $0.09$  &  $0.29$  &  $0.0$  &  $1.0$ \\
Age 25-34 &  $0.18$  &  $0.39$  &  $0.0$  &  $1.0$ \\
Age 35-44 &  $0.15$  &  $0.36$  &  $0.0$  &  $1.0$ \\
Age 45-64 &  $0.36$  &  $0.48$  &  $0.0$  &  $1.0$ \\
Age Over 65 &  $0.22$  &  $0.41$  &  $0.0$  &  $1.0$ \\
Did not Vote/Supp. Other Cand. in 2016 &  $0.18$  &  $0.38$  &  $0.0$  &  $1.0$ \\
Supported Clinton in 2016 &  $0.62$  &  $0.48$  &  $0.0$  &  $1.0$ \\
Supported Sanders in 2016 &  $0.17$  &  $0.38$  &  $0.0$  &  $1.0$ \\
Supported Trump in 2016 &  $0.03$  &  $0.16$  &  $0.0$  &  $1.0$ \\
HS or Less &  $0.21$  &  $0.41$  &  $0.0$  &  $1.0$ \\
Some College &  $0.38$  &  $0.49$  &  $0.0$  &  $1.0$ \\
College Graduate &  $0.41$  &  $0.49$  &  $0.0$  &  $1.0$ \\
Black &  $0.22$  &  $0.41$  &  $0.0$  &  $1.0$ \\
Latino &  $0.13$  &  $0.34$  &  $0.0$  &  $1.0$ \\
Other/Mixed Race/Ethnicity &  $0.07$  &  $0.26$  &  $0.0$  &  $1.0$ \\
White &  $0.58$  &  $0.49$  &  $0.0$  &  $1.0$ \\
Female &  $0.57$  &  $0.49$  &  $0.0$  &  $1.0$ \\
Campaign Knowledge &  $0.26$  &  $0.63$  &  $-1.0$  &  $1.0$ \\
Follows Politics &  $0.70$  &  $0.28$  &  $0.0$  &  $1.0$ \\
Strong Democrat &  $0.67$  &  $0.47$  &  $0.0$  &  $1.0$ \\
Trump Least Preferred &  $0.84$  &  $0.36$  &  $0.0$  &  $1.0$ \\
Intend to Vote &  $0.93$  &  $0.17$  &  $0.0$  &  $1.0$ \\
Registered Voter &  $0.95$  &  $0.22$  &  $0.0$  &  $1.0$ \\
Ideology = ideology &  $0.31$  &  $0.23$  &  $0.0$  &  $1.0$ \\
Political Interest &  $0.74$  &  $0.27$  &  $0.0$  &  $1.0$ \\
# Failed Attention Checks &  $0.86$  &  $0.91$  &  $0.0$  &  $3.0$ \\
Need for Cognition &  $0.66$  &  $0.17$  &  $0.0$  &  $1.0$ \\

> # descriptive covariates (main)----
> 
> X <- respondent_data[n_attention_fails >= 1 &
+     trump_least_preferred == 1 &
+     support_2016 != "Trump", rbind(
+   summarize("Age 18-24", as.numeric(age_category == "18-24")),
+   summarize("Age 25-34", as.numeric(age_category == "25-34")),
+   summarize("Age 35-44", as.numeric(age_category == "35-44")),
+   summarize("Age 45-64", as.numeric(age_category == "45-64")),
+   summarize("Age Over 65", as.numeric(age_category == "Over 65")),
+   summarize("Did not Vote/Supp. Other Cand. in 2016", as.numeric(support_2016 == "other/no one")),
+   summarize("Supported Clinton in 2016", as.numeric(support_2016 == "Clinton")),
+   summarize("Supported Sanders in 2016", as.numeric(support_2016 == "Sanders")),
+   summarize("Supported Trump in 2016", as.numeric(support_2016 == "Trump")),
+   summarize("HS or Less", as.numeric(education == "HS or Less")),
+   summarize("Some College", as.numeric(education == "Some College")),
+   summarize("College Graduate", as.numeric(education == "College Grad.")),
+   summarize("Black", as.numeric(race_ethnicity == "Black")),
+   summarize("Latino", as.numeric(race_ethnicity == "Latino")),
+   summarize("Other/Mixed Race/Ethnicity", as.numeric(race_ethnicity == "Other/Mixed")),
+   summarize("White", as.numeric(race_ethnicity == "White")),
+   summarize("Female", female),
+   summarize("Campaign Knowledge", campaign_knowledge),
+   summarize("Follows Politics", follows_politics),
+   summarize("Strong Democrat", strong_democrat),
+   summarize("Trump Least Preferred", trump_least_preferred),
+   summarize("Intend to Vote", vote_2020),
+   summarize("Registered Voter", registered_voter),
+   summarize("Ideology = ideology", ideology),
+   summarize("Political Interest", political_interest),
+   summarize("# Failed Attention Checks", n_attention_fails),
+   summarize("Need for Cognition", need_for_cognition))]

> print_rows(X, c(0, 2, 2, 1, 1))
Age 18-24 &  $0.11$  &  $0.31$  &  $0.0$  &  $1.0$ \\
Age 25-34 &  $0.21$  &  $0.40$  &  $0.0$  &  $1.0$ \\
Age 35-44 &  $0.16$  &  $0.36$  &  $0.0$  &  $1.0$ \\
Age 45-64 &  $0.33$  &  $0.47$  &  $0.0$  &  $1.0$ \\
Age Over 65 &  $0.20$  &  $0.40$  &  $0.0$  &  $1.0$ \\
Did not Vote/Supp. Other Cand. in 2016 &  $0.16$  &  $0.37$  &  $0.0$  &  $1.0$ \\
Supported Clinton in 2016 &  $0.68$  &  $0.47$  &  $0.0$  &  $1.0$ \\
Supported Sanders in 2016 &  $0.16$  &  $0.37$  &  $0.0$  &  $1.0$ \\
Supported Trump in 2016 &  $0.00$  &  $0.00$  &  $0.0$  &  $0.0$ \\
HS or Less &  $0.23$  &  $0.42$  &  $0.0$  &  $1.0$ \\
Some College &  $0.38$  &  $0.49$  &  $0.0$  &  $1.0$ \\
College Graduate &  $0.39$  &  $0.49$  &  $0.0$  &  $1.0$ \\
Black &  $0.25$  &  $0.43$  &  $0.0$  &  $1.0$ \\
Latino &  $0.15$  &  $0.36$  &  $0.0$  &  $1.0$ \\
Other/Mixed Race/Ethnicity &  $0.08$  &  $0.27$  &  $0.0$  &  $1.0$ \\
White &  $0.51$  &  $0.50$  &  $0.0$  &  $1.0$ \\
Female &  $0.58$  &  $0.49$  &  $0.0$  &  $1.0$ \\
Campaign Knowledge &  $0.18$  &  $0.66$  &  $-1.0$  &  $1.0$ \\
Follows Politics &  $0.73$  &  $0.27$  &  $0.0$  &  $1.0$ \\
Strong Democrat &  $0.71$  &  $0.45$  &  $0.0$  &  $1.0$ \\
Trump Least Preferred &  $1.00$  &  $0.00$  &  $1.0$  &  $1.0$ \\
Intend to Vote &  $0.93$  &  $0.17$  &  $0.0$  &  $1.0$ \\
Registered Voter &  $0.96$  &  $0.20$  &  $0.0$  &  $1.0$ \\
Ideology = ideology &  $0.30$  &  $0.24$  &  $0.0$  &  $1.0$ \\
Political Interest &  $0.75$  &  $0.26$  &  $0.0$  &  $1.0$ \\
# Failed Attention Checks &  $1.41$  &  $0.64$  &  $1.0$  &  $3.0$ \\
Need for Cognition &  $0.66$  &  $0.15$  &  $0.0$  &  $1.0$ \\

> # candidate evaluations (all) ----
> summarize <- function(v, x) {
+   data.table(
+     variable = v,
+     mean = mean(x),
+     sd = sd(x))
+ }

> X <- merge(
+   respondent_data[, rbind(
+     summarize("Biden", preferences_biden),
+     summarize("Bloomberg", preferences_bloomberg),
+     summarize("Buttigieg", preferences_buttigieg),
+     summarize("Klobuchar", preferences_klobuchar),
+     summarize("Sanders", preferences_sanders),
+     summarize("Warren", preferences_warren),
+     summarize("Yang", preferences_yang),
+     summarize("Trump", preferences_trump))],
+   respondent_data[, rbind(
+     summarize("Biden", electability_biden),
+     summarize("Bloomberg", electability_bloomberg),
+     summarize("Buttigieg", electability_buttigieg),
+     summarize("Klobuchar", electability_klobuchar),
+     summarize("Sanders", electability_sanders),
+     summarize("Warren", electability_warren),
+     summarize("Yang", electability_yang))],
+   by = "variable", all.x = TRUE, sort = FALSE)

> X <- merge(
+   X,
+   respondent_data[, rbind(
+     summarize("Biden", ranking_biden),
+     summarize("Bloomberg", ranking_bloomberg),
+     summarize("Buttigieg", ranking_buttigieg),
+     summarize("Klobuchar", ranking_klobuchar),
+     summarize("Sanders", ranking_sanders),
+     summarize("Warren", ranking_warren),
+     summarize("Yang", ranking_yang))],
+   by = "variable", all.x = TRUE, sort = FALSE)

> setnames(X, c("Candidate", "Mean", "SD", "Mean", "SD", "Mean", "SD"))

> print_rows(X, c(0, 0, 0, 0, 0, 1, 1))
Biden &  $71$  &  $27$  &  $74$  &  $24$  &  $2.8$  &  $1.9$ \\
Bloomberg &  $57$  &  $29$  &  $62$  &  $27$  &  $4.1$  &  $2.0$ \\
Buttigieg &  $52$  &  $28$  &  $53$  &  $27$  &  $4.5$  &  $1.7$ \\
Klobuchar &  $47$  &  $28$  &  $44$  &  $29$  &  $5.1$  &  $1.6$ \\
Sanders &  $69$  &  $28$  &  $69$  &  $26$  &  $3.1$  &  $1.9$ \\
Warren &  $64$  &  $28$  &  $62$  &  $27$  &  $3.4$  &  $1.8$ \\
Yang &  $46$  &  $28$  &  $44$  &  $29$  &  $5.0$  &  $1.7$ \\
Trump &  $12$  &  $24$  &  $NA$  &  $NA$  &  $NA$  &  $NA$ \\

> # candidate evaluations (main) ----
> X <- merge(
+   respondent_data[n_attention_fails >= 1 &
+       trump_least_preferred == 1 &
+       support_2016 != "Trump", rbind(
+     summarize("Biden", preferences_biden),
+     summarize("Bloomberg", preferences_bloomberg),
+     summarize("Buttigieg", preferences_buttigieg),
+     summarize("Klobuchar", preferences_klobuchar),
+     summarize("Sanders", preferences_sanders),
+     summarize("Warren", preferences_warren),
+     summarize("Yang", preferences_yang),
+     summarize("Trump", preferences_trump))],
+   respondent_data[, rbind(
+     summarize("Biden", electability_biden),
+     summarize("Bloomberg", electability_bloomberg),
+     summarize("Buttigieg", electability_buttigieg),
+     summarize("Klobuchar", electability_klobuchar),
+     summarize("Sanders", electability_sanders),
+     summarize("Warren", electability_warren),
+     summarize("Yang", electability_yang))],
+   by = "variable", all.x = TRUE, sort = FALSE)

> X <- merge(
+   X,
+   respondent_data[n_attention_fails >= 1 &
+       trump_least_preferred == 1 &
+       support_2016 != "Trump", rbind(
+     summarize("Biden", ranking_biden),
+     summarize("Bloomberg", ranking_bloomberg),
+     summarize("Buttigieg", ranking_buttigieg),
+     summarize("Klobuchar", ranking_klobuchar),
+     summarize("Sanders", ranking_sanders),
+     summarize("Warren", ranking_warren),
+     summarize("Yang", ranking_yang))],
+   by = "variable", all.x = TRUE, sort = FALSE)

> setnames(X, c("Candidate", "Mean", "SD", "Mean", "SD", "Mean", "SD"))

> print_rows <- function(x, digits) {
+   for (i in 1:nrow(x)) {
+     for (k in 1:ncol(x)) {
+       fmt <- paste0("%.0", digits[k], "f")
+       if (k == 1) {
+         cat(x[i, k, with = FALSE][[1]])
+       } else {
+         cat(" $", sprintf(fmt, x[i, k, with = FALSE][[1]]), "$ ", sep = "")
+       }
+       if (k == ncol(x)) {
+         cat("\\\\\n")
+       } else {
+         cat(" & ", sep = "")
+       }
+     }
+   }
+ }

> print_rows(X, c(0, 0, 0, 0, 0, 1, 1))
Biden &  $73$  &  $26$  &  $74$  &  $24$  &  $2.7$  &  $1.9$ \\
Bloomberg &  $58$  &  $29$  &  $62$  &  $27$  &  $4.2$  &  $1.9$ \\
Buttigieg &  $54$  &  $28$  &  $53$  &  $27$  &  $4.5$  &  $1.7$ \\
Klobuchar &  $50$  &  $27$  &  $44$  &  $29$  &  $5.2$  &  $1.6$ \\
Sanders &  $73$  &  $26$  &  $69$  &  $26$  &  $3.0$  &  $1.9$ \\
Warren &  $67$  &  $26$  &  $62$  &  $27$  &  $3.3$  &  $1.7$ \\
Yang &  $50$  &  $28$  &  $44$  &  $29$  &  $5.1$  &  $1.7$ \\
Trump &  $6$  &  $13$  &  $NA$  &  $NA$  &  $NA$  &  $NA$ \\
